/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} k
 * @return {ListNode}
 */
export var rotateRight = function (head, k) {
  if (!head) return null
  if (!k) return head
  const stack = []
  let cur = head
  while (cur) {
    stack.push(cur)
    cur = cur.next
  }
  const deep = stack.length
  k = k % deep
  if (k === 0) return head
  // 拆分成 [0. deep - k - 1] 和 [deep - k, deep - 1] 两部分
  stack[deep - 1].next = head
  stack[deep - k - 1].next = null
  return stack[deep - k]
}
